<?php
namespace app\admin666\controller;

use think\Db;
use think\Validate;

class Flink extends Admin
{
    /**
    * 友情链接 
    * @date: 2018年8月18日 下午5:25:03
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function index(){
        $status = trim(input('status',''));
        $q = trim(input('get.q',''));
    
        $map = [];
    
        if($q != ''){
            $map['title'] = ['like',"%{$q}%"];
        }
         
        if($status != ''){
            $map['status'] = $status;
        }
         
        $order="id desc";
    
        $list = Db::name('flink')->where($map)->paginate(15,false,['query'=>input('get.')]);
        $page = $list->render();
    
        $this->assign('_list', $list);
        $this->assign('_page', $page);
    
        return $this->fetch();
    }
    
    /**
     * 添加
     * @date: 2018年6月7日 上午10:32:54
     * @author: xiaocui
     * @param: $GLOBALS
     * @return:
     */
    public function add(){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
            ];
    
            $msg = [
                'title.require' => '请填写链接名称',
            ];
            $validate = new Validate($rule,$msg);
    
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
    
            $model = model('Flink');
            $data = input('post.');
            if($model->save($data)){
                $this->success('添加成功！','');
            }else{
                $this->error($model->getError());
            }
    
        }else{
            return $this->fetch('edit');
        }
    }
    
    /**
     * 编辑
     * @date: 2018年6月7日 上午10:50:46
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function edit($id=0){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
            ];
    
            $msg = [
                'title.require' => '请填写链接名称',
            ];
            $validate = new Validate($rule,$msg);
    
            $model = model('Flink');
            $data = input('post.');
            if($model->save($data,['id'=>$id])){
                $this->success('编辑成功！','');
            }else{
                if($model->getError()){
                    $this->error($model->getError());
                }else{
                    $this->success('编辑成功！','');
                }
            }
        }else{
            $info = model('Flink')->find($id);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    
    /**
     * 删除
     * @date: 2018年6月7日 上午10:50:56
     * @author: xiaocui
     * @param: $GLOBALS
     * @return:
     */
    public function del($id=0){
        $model = model('Flink');
        if(($model->destroy($id))){
            //记录行为
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    
    /**
     * 状态
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function status($id,$status){
        $id = intval($id);
        $status = intval($status);
        $model = model('Flink');
        if($model->save(['status'=>$status],['id'=>$id])){
            $this->success('修改成功','');
        }else{
            $this->error('修改失败！');
        }
    }
    
    
    /**
     * 排序
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function sort(){
        if(request()->isPost()){
            $post = input('post.');
            if(isset($post['ids']) && isset($post['sort'])){
                $data = [];
                foreach ($post['ids'] as $key=>$val){
                    $data[$key]['id'] = $val;
                    $data[$key]['sort'] = intval($post['sort'][$key]);
                }
    
                if(model('Flink')->saveAll($data,true)){
                    $this->success('排序成功！');
                }
    
            }
            $this->error('提交失败！');
        }
    }
}
